package com.avcdata.aoscustommade.main;

import android.app.Application;
import android.content.Context;
import android.os.Handler;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.http.AvcHttpUtils;
import com.avcdata.national.network.BaseLog;
import com.mob.MobApplication;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

import cn.sharesdk.framework.ShareSDK;


/**
 * Created by c.c on 2017/2/26.
 * 应用Application基类
 */
public class MyApplication extends MobApplication {
    public static MyApplication softApplication;
    private Handler handler;
    private ImageLoader iLoader;


    //构造方法
    public MyApplication() {

    }

    @Override
    public void onCreate() {
        softApplication = this;
        super.onCreate();

        //检测OOM
//        LeakCanary.install(this);
//        //初始化极光服务
//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);
        //
        initImageLoader();
        // 初始化网络请求
        AvcHttpUtils.init(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        MultiDex.install(this);
    }

    //实例化一次
    public synchronized static MyApplication getInstance() {
        if (null == softApplication) {
            softApplication = new MyApplication();
        }
        return softApplication;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }


    private void initImageLoader() {
        File cacheDir = StorageUtils.getOwnCacheDirectory ( getApplicationContext (),
                "PoketCompass/image/" );
        // 默认配置对象
        DisplayImageOptions.Builder oBuilder = new DisplayImageOptions.Builder ()
                .imageScaleType ( ImageScaleType.EXACTLY )// 图片的缩放类
                .cacheInMemory ( true ).cacheOnDisc ( true );// 缓存到
//                .showStubImage ( R.drawable.ic_launcher )
//                .showImageOnLoading ( R.drawable.onloading);
        // .showStubImage(R.drawable.ic_launcher)// 加载前的默认图片
        // .showImageOnLoading(R.drawable.default_head)// 加载时默认图片
        // .showImageForEmptyUri(R.drawable.default_head) // URL为空会显示该图片
        // .showImageOnFail(R.drawable.default_head); // 加载图片出现问题，会显示该图
        DisplayImageOptions options = oBuilder.build ();

        ImageLoaderConfiguration.Builder cBuilder = new ImageLoaderConfiguration.Builder (
                getApplicationContext () ).defaultDisplayImageOptions ( options );

        ImageLoaderConfiguration config = cBuilder
                .threadPriority ( Thread.NORM_PRIORITY - 2 )

                .denyCacheImageMultipleSizesInMemory ()
                .discCacheFileNameGenerator ( new Md5FileNameGenerator() )
                .tasksProcessingOrder ( QueueProcessingType.LIFO )
                .denyCacheImageMultipleSizesInMemory ()
                .memoryCache ( new LruMemoryCache( 2 * 1024 * 1024 ) )
                .memoryCacheSize ( 2 * 1024 * 1024 ).memoryCacheSizePercentage ( 13 )
                .discCache ( new UnlimitedDiscCache( cacheDir ) )//获取系统SD卡缓存目录
                .discCache ( new UnlimitedDiscCache ( cacheDir ) )
//                 .defaultDisplayImageOptions(options_new) // default
                .writeDebugLogs ().build ();
        ImageLoader.getInstance ().init ( config );
        iLoader = ImageLoader.getInstance ();
        iLoader.init ( ImageLoaderConfiguration.createDefault ( getApplicationContext () ) );
    }

    /**
     * 强制升级时调用的退出方法
     */
    public void exit() {
        android.os.Process.killProcess(android.os.Process.myPid());
    }

}
